package org.zh.home.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.zh.home.dao.BaseDao;
import org.zh.home.dao.IGroupDao;
import org.zh.home.entity.Group;
import org.zh.home.model.Pager;
import org.zh.home.model.Param;
import org.zh.home.model.Query;
import org.zh.home.utils.QueryUtils;

@Repository
public class GroupDao extends BaseDao<Group> implements IGroupDao {
	
	@Override
	public List<Group> list(List<Query> model) {
		Param param = QueryUtils.generateHQLBase("from Group","g", model);
		return super.list(param.getQuery(), param.getValues().toArray());
	}

	@Override
	public Pager<Group> find(List<Query> model) {
		Param param = QueryUtils.generateHQLBase("from Group", "g", model);
		return super.find(param.getQuery(), param.getValues().toArray());
	}

	@Override
	public List<Group> listChildren(int groupId) {
		String hql = "from　Group g where g.parent.id = "+groupId;
		return super.list(hql);
	}

	@Override
	public List<Integer> listIds(List<Query> model) {
		Param param = QueryUtils.generateHQLBase("select g.id from sdf_group g", "g", model);
		return super.listBySql(param.getQuery(), param.getValues().toArray(), Integer.class, false);
	}

	@Override
	public void deleteGroupColumn(int id) {
		String hql = "delete GroupColumn gc where gc.id = "+id;
		super.updateByHql(hql);
	}

	@Override
	public void updateUserGroupRole(int id) {
		String hql = "update UserGroupRole ugr set ugr.groupId = 0,ugr.groupName = '' where ugr.groupId = "+id;
		super.updateByHql(hql);
	}
}
